package edu.msg.jthr.backend.bean;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.Role;
import edu.msg.jthr.backend.repository.exception.RepositoryException;
import edu.msg.jthr.backend.repository.service.RoleRepository;
import edu.msg.jthr.backend.service.RoleService;

@Stateless (name = "RoleServiceBean", mappedName = "ejb/RoleService")
public class RoleServiceBean implements RoleService {

	@EJB
	private RoleRepository roleRepository;
	private static Logger LOG = LoggerFactory
			.getLogger(CandidateServiceBean.class);

	@Override
	public List<Role> getAll() throws ServiceException {
		List<Role> roleList = new ArrayList<Role>();
		
		try {
			roleList = roleRepository.getAll();
			return roleList;
		} catch (RepositoryException e) {
			LOG.error("Get all roles error!" + e.getMessage());
			throw new ServiceException("Role getById error!", e);
		}
	}

	@Override
	public Role getById(Long id) throws ServiceException {
		try {
			return roleRepository.getById(id);
		} catch (RepositoryException e) {
			LOG.error("Role getById error!" + e.getMessage());
			throw new ServiceException("Role getById error!", e);
		}
	}

	@Override
	public void create(Role obj) throws ServiceException {
		try {
			roleRepository.create(obj);
		} catch (RepositoryException e) {
			LOG.error("Role creating failure!" + e.getMessage());
			throw new ServiceException("Role creating failure!", e);
		}
	}

	@Override
	public Role update(Role obj) throws ServiceException {
		try {
			return roleRepository.update(obj);
		} catch (RepositoryException e) {
			LOG.error("Role update failure!" + e.getMessage());
			throw new ServiceException("Role update failure!", e);
		}
	}

	@Override
	public void delete(Role obj) throws ServiceException {
		try {
			roleRepository.delete(obj);
		} catch (RepositoryException e) {
			LOG.error("Role delete failure!" + e.getMessage());
			throw new ServiceException("Role delete failure!", e);
		}
	}

}
